/*******************************************
 * Copyright 2007 Aphrodite Framework for javaME .
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * Email : owner@aphroditeme.dev.java.net
 * Contributors:
 *     Kent Elton - initial API and implementation
 *******************************************/

package com.aphrodite.ui.widgets;

import javax.microedition.lcdui.Graphics;

/**
 * <p>
 * StopWatch widget
 * </p>
 * Good to benchmark and test j2me operations. With an accuracy of 90%.
 * 
 * @author kentelt
 * 
 */
public final class StopWatch extends TextEffect {

	private volatile int hour;
	private boolean isCountDown;
	private volatile int min;
	private volatile int sec;
	private char sep = ':';

	private volatile StringBuffer time = new StringBuffer();

	public StopWatch(Theme theme) {
		super(theme, 0, 0);
		setTime(System.currentTimeMillis());
		// updateModel();
		super.effectInterval = 950;
	}

	protected void doAniTask() {
		if (!isCountDown) {
			sec++;
			if (sec == 60) {
				sec = 0;
				min++;
			}
			if (min == 60) {
				hour++;
				min = 0;
			}
		} else {
			sec--;
			if (sec == 0) {
				sec = 60;
				min--;
			}
			if (min == 0) {
				hour--;
				min = 60;
			}
		}
		setFormat();
	}

	/**
	 * Get the time recorded on the stop watch
	 * 
	 * @return number of milliseconds
	 */
	public long getTime() {
		return (hour * 3600000 + min * 60000 + sec * 1000);
	}

	public void render(Graphics g) {
		if (!isVisible)
			return;
		capture(g, width, height, true);
		g.setColor(theme.fgColor);
		if (theme.bmFont != null)
			theme.bmFont.drawString(g, time.toString(), 2, 2, 0);
		else {
			g.setFont(theme.widgetFont);
			g.drawString(time.toString(), 2, 2, 0);
		}
		release(g, width, height, true);

	}

	public void reset() {
		hour = 0;
		sec = 0;
		min = 0;
		reDrawWidget();
		stateChanged(this);
	}

	public synchronized final void setAnimationInterval(int interval) {}

	/**
	 * Use the stop watch as a count down timer
	 * 
	 * @param isCountDownTimer
	 */
	public void setAsCountDown(boolean isCountDownTimer) {
		this.isCountDown = isCountDownTimer;
	}

	private final void setFormat() {
		time.delete(0, time.length());
		time = new StringBuffer().append(hour).append(sep).append(min).append(sep).append(sec);
	}

	public synchronized void setSeparatorChar(char separator) {
		this.sep = separator;
		updateModel();
		reDrawWidget();
	}

	public void setTime(long time) {
		java.util.Calendar cal = java.util.Calendar.getInstance();
		cal.setTime(new java.util.Date(time));
		min = cal.get(java.util.Calendar.MINUTE);
		sec = cal.get(java.util.Calendar.SECOND);
		hour = cal.get(java.util.Calendar.HOUR);
		updateModel();
		reDrawWidget();
		stateChanged(this);
	}

	protected void updateModel() {
		if (theme.bmFont != null) {
			this.width = theme.bmFont.stringWidth("88" + sep + "88" + sep + "88") + 4;
			this.height = theme.bmFont.getHeight() + 4;
		} else {
			this.width = theme.widgetFont.stringWidth("88" + sep + "88" + sep + "88") + 4;
			this.height = theme.widgetFont.getHeight() + 4;
		}
		setFormat();
	}
}
